package com.cdtu.outtake.common;

import com.cdtu.outtake.domain.enums.ResultType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author : shl
 * @Date 2023/12/22 21:33
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    private int code;
    private String msg;
    private T data;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> ok() {
        return new Result<>(ResultType.SUCCESS.getCode(), ResultType.SUCCESS.getMsg());
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultType.SUCCESS.getCode(), ResultType.SUCCESS.getMsg(), data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResultType.ERROR.getCode(), ResultType.ERROR.getMsg());
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(ResultType.ERROR.getCode(), msg);
    }

}
